package com.redrose.demo.base;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public abstract class BaseSingleAdapter<T extends ViewDataBinding,Y> extends RecyclerView.Adapter<BaseSingleAdapter.BaseViewHolder<T>> {
    protected List<Y> mList;

    public BaseSingleAdapter(List<Y> list) {
        this.mList = list;
    }

    @NonNull
    @Override
    public BaseViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        T binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                getLayoutRes(), parent, false);
        return new BaseViewHolder<>(binding);
    }

    protected abstract int getLayoutRes();

    @Override
    public int getItemCount() {
     return mList == null ? 0 : mList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        bindView(holder.bind, mList.get(position),position);
    }

    protected abstract <D extends ViewDataBinding> void  bindView(D binding, Y bean,int position);

    static class BaseViewHolder<D extends ViewDataBinding> extends RecyclerView.ViewHolder {
        D bind;
        public BaseViewHolder(@NonNull D bind) {
            super(bind.getRoot());
            this.bind = bind;
        }
    }
}
